<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}


if(!empty($_POST['sub']))
{
    $chefid = (int)$_POST['id'];
    $orderDate = $_POST['orderDate'];
    $orderPrice = (int)$_POST['orderPrice'];
    $orderGoods= $_POST['orderGoods'];
    $orderTao= $_POST['orderTao'];
    $orderDate = $_POST['orderDate'];
    $dishware = $_POST['dishware'];
    $waiter = $_POST['waiter'];
    $guest_name  = $_POST['guest_name'];
    $tel = $_POST['tel'];
    $address = $_POST['address'];
    $remark = $_POST['remark'];
    $coupon = $_POST['coupon'];//优惠劵
    $orderRemark = $_POST['orderRemark'];//菜的备注
    $canType = $_POST['canType'];
    $custnums = $_POST['custnums'];
    $couponPrice = $_POST['couponPrice'];



    if($chefid<=0)
    {
        jumpajax('300','名厨(名店)不能为空!','');
    }
    if(empty($orderDate))
    {
        jumpajax('300','请选择档期!','');
    }
    if($canType == 0)
    {
        jumpajax('300','请选择午餐或晚餐!','');
    }
    if($orderPrice <= 0)
    {
        jumpajax('300','请选择菜','');
    }
    /*orderTaoArray
(
    [0] => Array
        (
            [0] => 7
            [1] => 888
            [2] => 2
        )

)
     * */
    $orderTao = json_decode($orderTao, true);
    if($orderTao[0][0] > 0)
    {

        $geniusid = $orderTao[0][0];

        //获取套餐的菜
        $genius = $ezhand->select("shopping3_genius","dishes","id=".$geniusid);
        //pp($genius);
        $dishes = unserialize($genius);
        /**
         * Array
        (
        [id] => Array
        (
        [0] => 13
        [1] => 15
        [2] => 14
        [3] => 10
        [4] => 12
        )

        [title] => Array
        (
        [0] => 烤乳鸽
        [1] => 扇贝
        [2] => 富贵虾
        [3] => 清蒸鱼
        [4] => 榴莲酥
        )

        )
         */
        $ids = implode(',',$dishes['id']);
        $goodsArr = $ezhand->select_all('shopping3_goods','id,title,productprice','id in ('.$ids.')');
        //pp($goodsArr);
        /**
        Array
        (
        [0] => Array
        (
        [id] => 15
        [title] => 扇贝
        [productprice] => 85
        )

        [id] => Array
        (
        [id] => 12
        [title] => 榴莲酥
        [productprice] => 38
         [num]=>2
        )
         */
        foreach($goodsArr as $val)
        {
            $taoArr[$val['id']]['goodsid'] = $val['id'];
            $taoArr[$val['id']]['total'] = $orderTao[0][2];
            $taoArr[$val['id']]['price'] = $val['productprice'];
        }

    }
    else{
        $geniusid = 0;
    }
    /**
    Array
    (
    [0] => Array
    (
    [0] => 9
    [1] => 48
    [2] => 3
    )

    [1] => Array
    (
    [0] => 12
    [1] => 38
    [2] => 3
    )

    [2] => Array
    (
    [0] => 13
    [1] => 48
    [2] => 3
    )

    [3] => Array
    (
    [0] => 14
    [1] => 128
    [2] => 3
    )

    )
     */
    $orderGoods = json_decode($orderGoods, true);
    if(!empty($orderGoods))
    {

        foreach($orderGoods as $val)
        {
            $orderGoodsNew[$val[0]]['goodsid'] = $val[0];
            $orderGoodsNew[$val[0]]['total'] = $val[2];
            $orderGoodsNew[$val[0]]['price'] = $val[1];

        }
    }
    //pp($orderGoodsNew);
    //pp($taoArr);
    if(!empty($orderGoodsNew) && !empty($taoArr))
    {

            foreach($orderGoodsNew as $key=>$val)
            {
               if(!empty($taoArr[$key]))
               {
                   $orderGoodsNew[$key]['total'] += $taoArr[$key]['total'];
               }
            }
        $orderGoodsNew = $orderGoodsNew+$taoArr;

    }
    elseif(!empty($orderGoodsNew))
    {

    }
    else{
        $orderGoodsNew = $taoArr;
    }
    //pp($orderGoodsNew);
    //pp($orderRemark);
    if(!empty($orderRemark))
    {
        $orderRemark = explode('##', $orderRemark);
        foreach($orderRemark as $val)
        {
            $v = explode('||', $val);
            if(!empty($orderGoodsNew[$v[0]]))
            {
                $orderGoodsNew[$v[0]]['description'] = $v[1];
            }
        }
    }
    //保存新订单
    $data = array(
        'weid' => $weid,
        'from_user' => $_SESSION['EZ_Admin']['id'],//登录后台的id
        'ordersn' => date('md') .sprintf("%02d", $_SESSION['EZ_Admin']['id']) .rand(1000,9999),

        'status' => 0,
        'sendtype' => 0,
        'paytype' => 0,

        'tel'=>$tel,
        'guest_name'=>$guest_name,
        'sex'=>'',//没性别
        'guest_address'=>$address,

        'time_day'=>$orderDate,
        'seat_type'=>0,
        'totalnum' => 0,
        'totalprice' => 0,
        'amount' => $orderPrice,
        'remark'=>$remark,
        'custnums'=>$custnums,
        'createtime' => time(),
        'secretid'=>rand(1000,9999),
        //打印状态
        'print_sta'=>0,
        'desk'=>$_GPC['desk'],//m没用
        'order_type'=>$_GPC['order_type'],//没用
        'shop_id'=>$chefid,
        //增加的字段:午餐，服务员，餐具，套餐名
        'can_type'=>$canType,
        'dishware'=>$dishware,
        'waiter'=>$waiter,
        'geniusid'=>$geniusid,//如果选套餐就是套餐id，不选为0



    );
    $orderid = $ezhand->insert('shopping3_order',$data);

    if($orderid > 0)
    {
        //优惠劵使用
        if($couponPrice > 0)
        {
            $ezhand->update('coupon_given',array('orderid'=>$orderid,'status'=>1,'usetime'=>time()), "coupon_sn='$coupon'");
        }
        //档期修改
        $ezhand->update('shopping3_schedule',array('orderid'=>$orderid), "chefid='$chefid' and sdate='$orderDate'");

        foreach($orderGoodsNew as $val)
        {
                $ezhand->insert('shopping3_order_goods', array(
                    'weid' => $weid,
                    'goodsid' => $val['goodsid'],
                    'orderid' => $orderid,
                    'total' => $val['total'],
                    'price' => $val['price'],
                    'description'=>$val['description'],
                    'createtime' => time(),
                ));
        }
        jumpajax('200','下单成功','');

    }
    jumpajax('300','下单失败','');


}
else{
    $chefid = intval($_GET['id']);
    $d=date("Y-m-d");
    if($chefid==0){
        echo "请选择名店/名厨！";
        exit;
    }
    $chef=$ezhand->select("shopping3_chef","*","status=1 and id=".$chefid);

    $cuisinelist=array(
        '0'=>array('name'=>'请选择','img'=>''),
        '1'=>array('name'=>'鲁菜','img'=>''),
        '2'=>array('name'=>'粤菜','img'=>'images/yue.png'),
        '3'=>array('name'=>'川菜','img'=>'images/chuan.png'),
        '4'=>array('name'=>'湘菜','img'=>''),
        '5'=>array('name'=>'闽菜','img'=>''),
        '6'=>array('name'=>'浙菜','img'=>''),
        '7'=>array('name'=>'苏菜','img'=>''),
        '8'=>array('name'=>'徽菜','img'=>'images/hui.png'),
    );

    $genius_list = $ezhand->select_all("shopping3_genius","id,thumb,title,marketprice,goodsid,totalprice,dishes"," status=1 and shop_id=".$chefid." order by displayorder");
    foreach ($genius_list as $key=>$val)
    {
        $genius_list[$key]['dishes']  = unserialize($val['dishes']);
    }



//pp($genius_list);
    $datrow=$ezhand->select_all("shopping3_schedule","*","chefid=".$chefid);

    $goods_list = $ezhand->select_all('shopping3_goods' ,"*","status=1 and shop_id=".$chefid." order by pcate");

    $goodsnew = array();
//$goodstype = array();
    foreach($goods_list as $val)
    {
        $goodsnew[$val['pcate']][] = $val;
    }

    $goodstype_list = $ezhand->select_all('shopping3_category' ,"*");
    $goodst = array();
    foreach($goodstype_list as $val)
    {
        $goodst[$val['id']] = $val['name'];
    }

}











?>
